/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_01_elasticity.do
Date: October 2022

Description: Reproduces Table 1: Estimating the composite elasticity, \beta.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/table_01_elasticity.log",replace
*-------------------------------------------------------------------------------




************************ TABLE 1  **************************
local grvar0 "LP_ppp_emp_pwt" 
global ctryrel FR
local yy "2016"
*minimum number of sectors allowed per year-isocode*/
local nn=10 
local sample `" isocode=="" "'


*Restricting the set of isocode-years for those pairs with less than `nn' sectors
*----------------------------------------------------------
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta"

gsort year type sector -DA
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 


*Restricting the set of isocode-years for those pairs with less than `nn' sectors
*----------------------------------------------------------
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear
keep if year==`yy'
drop if sector1==sector
tab type
replace DA=. if num_aff<3
replace DP=. if num_aff<3
levelsof sector, local(seclist)

merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
capture rename DA D_A
capture rename DP D_P
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  
display "`drop if `sample''"


*Bring some aggregate control variables
*----------------------------------------------------------
merge m:1 year isocode sector using "${data}/aggregates_tfp_lp_klems.dta"
drop if _merge==2
drop _merge
gen k_y=ln_ck-ln_rgdpo

merge m:1 year isocode using "${data}/additional_agg_indicators.dta"
drop if _merge==2
drop _merge

gen ln_patent=ln(patent_total)
gen ln_patent_pc=ln_patent-ln_pop
gen ln_trademark=ln(trademark_application)
gen ln_trademark_pc=ln_trademark-ln_pop
gen ln_researcher_permm=ln(Researchers_RD_permm)
gen ln_mobile_per100=ln(mobile_per100) 
gen ln_broadband_per100=ln(broadband_per100) 
gen ln_electric_pc=ln(electric_percapita) 
gen ln_freshwater_pc=ln(freshwater_percapita) 
gen ln_air_transp=ln(air_transp) 
gen ln_container_port=ln(container_port)
gen ln_schooling=ln(schooling)
egen gov_index=rowmean(PoliticalStability GovernmentEffectiveness RegulatoryQuality RuleofLaw ControlofCorruption)
egen gov_rank_index=rowmean(PoliticalStability_rank GovernmentEffectiveness_rank RegulatoryQuality_rank RuleofLaw_rank ControlofCorruption_rank)

tempfile temp
save `temp', replace 	


*Creating Table 1
*----------------------------------------------------------
foreach cover in alleconomy manuf serv {
display "`cover'"

clear all
use `temp', clear
local rr "" 
local var_dep "ln_`grvar0'"
local var D_A
gsort type sector -D_A
local clusterlist "isocode sector"
local abslist "sector"

if "`cover'"=="alleconomy" {
display "Cover: All Economy"
}
if "`cover'"=="manuf" {
display "Cover: Manufacturing"
keep if type=="manuf"
}
if "`cover'"=="serv" {
display "Cover: Services"
keep if type=="serv"
}


*Baseline regression (pooled across sectors)
*----------------------------------------
reghdfe `var_dep' `var', abs(`abslist')  vce(cluster `clusterlist')
mat b = e(b)
mat V = e(V)
global beta: display %-03.2fc round(b[1,1],0.01)
global se_beta: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$beta, $se_beta, $rho, $se_rho"
outreg2 using "${rmain}/table_01.xls", br ctitle(OLS levels (model 1), type: `cover',  var_dep: `var_dep', FE: `abslist', cluster (`clusterlist')) append 


*Controlling for K/Y and HC (pooled across sectors) 
*----------------------------------------
reghdfe `var_dep' `var' k_y ln_hc, abs(`abslist')  vce(cluster `clusterlist')
mat b = e(b)
mat V = e(V)
global beta: display %-03.2fc round(b[1,1],0.01)
global se_beta: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$beta, $se_beta, $rho, $se_rho"
outreg2 using "${rmain}/table_01.xls", br ctitle(OLS levels (model 2), type: `cover',  var_dep: `var_dep', FE: `abslist', cluster (`clusterlist')) append 



*Controlling for K/Y, HC and ln_mobile_per100 and Governance Indicator (pooled across sectors)
*----------------------------------------
reghdfe `var_dep' `var' k_y ln_hc gov_rank_index ln_mobile_per100, abs(`abslist')  vce(cluster `clusterlist')
mat b = e(b)
mat V = e(V)
global beta: display %-03.2fc round(b[1,1],0.01)
global se_beta: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$beta, $se_beta, $rho, $se_rho"
outreg2 using "${rmain}/table_01.xls", br ctitle(OLS levels (model 3), type: `cover',  var_dep: `var_dep', FE: `abslist', cluster (`clusterlist')) append 

}
*

log close



